package cn.itrip.auth.controller;

import cn.itrip.auth.service.ItripUserService;
import cn.itrip.auth.service.TokenService;
import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.beans.vo.ItripTokenVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.ErrorCode;
import cn.itrip.common.MD5;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;

@Controller
@RequestMapping("/api")
public class LoginController {
    @Resource
    private TokenService tokenService;
    @Resource
    private ItripUserService itripUserService;
    @ApiOperation(value = "用户登录",httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = Dto.class,notes = "根据用户名、密码进行统一认证")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form",required = true,value = "用户名",name = "name",defaultValue = "fxy1231206@163.com"),
            @ApiImplicitParam(paramType = "form",required = true,value = "密码",name = "password",defaultValue = "123456"),
    })
@RequestMapping(value = "/dologin",method = RequestMethod.POST,produces = "application/json")
    public @ResponseBody Dto login(@RequestParam String name,
          @RequestParam String password,
          HttpServletRequest request){
        try{
            ItripUser user=itripUserService.login(name, MD5.getMd5(password,32));
            if (EmptyUtils.isNotEmpty(user)){
                String userAgent=request.getHeader("user-Agent");
                String token=tokenService.generateToken(userAgent,user);
                tokenService.save(token,user);
                ItripTokenVO vo=new ItripTokenVO(token, Calendar.getInstance().getTimeInMillis()+2*60*60*1000,
                        Calendar.getInstance().getTimeInMillis());
                return DtoUtil.returnDataSuccess(vo);
            }else {
                return DtoUtil.returnFail("用户密码错误", ErrorCode.AUTH_AUTHENTICATION_FAILED);
            }
        }catch (Exception e){
              e.printStackTrace();
              return DtoUtil.returnFail(e.getMessage(),ErrorCode.AUTH_AUTHENTICATION_FAILED);
        }

    }
    @ApiOperation(value = "用户注销",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = Dto.class,notes = "客户端需要在header中发送token")
    @ApiImplicitParam(paramType = "header",required = true,name = "token",value = "用户认证凭据",defaultValue = "")
    @RequestMapping(value = "/logout",method =RequestMethod.GET,produces ="application/json",headers = "token")
    public @ResponseBody Dto logout(HttpServletRequest request)
    {
        String token=request.getHeader("token");
        try{
            if (tokenService.validate(request.getHeader("user-agent"),token)){
                tokenService.delete(token);
                return DtoUtil.returnSuccess();
            }else {
                return DtoUtil.returnFail("token无效",ErrorCode.AUTH_TOKEN_INVALID);
            }
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("退出失败",ErrorCode.AUTH_TOKEN_INVALID);
        }

    }
}
